package com.tanhua.admin.module.dynamicmanage.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.tanhua.admin.module.dynamicmanage.service.MessageDetailsService;
import com.tanhua.admin.module.dynamicmanage.vo.YzcMessagesVo;
import com.tanhua.common.mapper.PublishInfoMapper;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.PublishInfo;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.pojo.Publish;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageDetailsServiceImpl implements MessageDetailsService {

    @Autowired
    private PublishInfoMapper publishInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public YzcMessagesVo messageDetails(String id) {
        //创建要返回的数据对象
        YzcMessagesVo yzcMessagesVo = new YzcMessagesVo();
        //查询数据 拼接到messagesVo中
        //查询mongodb quanzi_publish
        Publish publish = mongoTemplate.findById(new ObjectId(id), Publish.class);
        if(ObjectUtil.isEmpty(publish)){
            //不存在  设置假数据
            yzcMessagesVo.setText("你说嘴巴嘟嘟,我嘟嘟嘟~~~,嘟一下你就回来呀~~~~~~~~~");
            List<String> medias = new ArrayList<>();
            medias.add("https://wx2.sinaimg.cn/mw1024/8958b2c3gy1gpcooghtuxj20u0140alm.jpg");
            medias.add("https://wx3.sinaimg.cn/mw1024/8958b2c3gy1gpcood4c6wj20u01ar7k1.jpg");
            medias.add("https://wx1.sinaimg.cn/mw1024/8958b2c3gy1gpcooj04alj20u0140tkq.jpg");
            medias.add("https://wx3.sinaimg.cn/mw1024/8958b2c3gy1gp97viw75nj20u0140k41.jpg");
            yzcMessagesVo.setMedias(medias);
        }else {
            yzcMessagesVo.setText(publish.getText());
            yzcMessagesVo.setMedias(publish.getMedias());
        }
        //查询mysql publish_info表
        //构建条件
        QueryWrapper<PublishInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("publish_id", id);
        //查询
        PublishInfo publishInfo = publishInfoMapper.selectOne(wrapper);
        if(ObjectUtil.isNotEmpty(publishInfo)){
            yzcMessagesVo.setId(publishInfo.getPublishId());
            yzcMessagesVo.setCreateDate(publishInfo.getCreateDate());
            yzcMessagesVo.setState(publishInfo.getState());
            yzcMessagesVo.setTopState(publishInfo.getTopState());
            yzcMessagesVo.setReportCount(publishInfo.getReportCount());
            yzcMessagesVo.setLikeCount(publishInfo.getLikeCount());
            yzcMessagesVo.setCommentCount(publishInfo.getCommentCount());
            yzcMessagesVo.setForwardingCount(publishInfo.getForwardingCount());
        }

        //查询mysql publish_info表
        //构建条件
        QueryWrapper<UserInfo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id", publishInfo.getUserId());
        //查询
        UserInfo userInfo = userInfoMapper.selectOne(wrapper1);
        if(ObjectUtil.isNotEmpty(userInfo)){
            yzcMessagesVo.setNickname(userInfo.getNickName());
            yzcMessagesVo.setUserId(userInfo.getUserId());
            yzcMessagesVo.setUserLogo(userInfo.getLogo());
        }

        return yzcMessagesVo;
    }
}
